package org.br.erp.rfq.po.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.service.IScmbillcoderuleService;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.rfq.po.entity.Scmpo;
import org.br.erp.rfq.po.entity.Scmpoattach;
import org.br.erp.rfq.po.entity.Scmpoitem;
import org.br.erp.rfq.po.mapper.ScmpoattachMapper;
import org.br.erp.rfq.po.mapper.ScmpoitemMapper;
import org.br.erp.rfq.po.mapper.ScmpoMapper;
import org.br.erp.rfq.po.service.IScmpoService;
import org.br.erp.rfq.quo.entity.Scmquotation;
import org.br.erp.rfq.quo.service.IScmquotationService;
import org.br.erp.rfq.xbj.entity.Scmxjd;
import org.br.erp.rfq.xbj.entity.Scmxjditem;
import org.br.erp.rfq.xbj.service.IScmxjdService;
import org.br.erp.rfq.xbj.service.IScmxjditemService;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 采购订单
 * @Author: jeecg-boot
 * @Date:   2023-09-17
 * @Version: V1.0
 */
@Service
public class ScmpoServiceImpl extends ServiceImpl<ScmpoMapper, Scmpo> implements IScmpoService {

	@Autowired
	private ScmpoMapper scmpoMapper;
	@Autowired
	private ScmpoitemMapper scmpoitemMapper;

	@Autowired
	private IScmquotationService scmquotationService;

	@Autowired
	private IScmbillcoderuleService scmbillcoderuleService;

	@Autowired
	private IScmxjdService scmxjdService;

	@Autowired
	private IScmxjditemService scmxjditemService;

	@Autowired
	private ScmpoattachMapper scmpoattachMapper;

	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Override
	public void saveMain(Scmpo scmpo, List<Scmpoitem> scmpoitemList, List<Scmpoattach> scmpoattachList) {
		scmpoMapper.insert(scmpo);
		if(scmpoitemList!=null && scmpoitemList.size()>0) {
			for(Scmpoitem entity:scmpoitemList) {
				//外键设置
				entity.setParentid(scmpo.getId());
				scmpoitemMapper.insert(entity);
			}
		}
		if(scmpoattachList!=null && scmpoattachList.size()>0) {
			for(Scmpoattach entity:scmpoattachList) {
				//外键设置
				entity.setParentid(scmpo.getId());
				scmpoattachMapper.insert(entity);
			}
		}
	}

	@Override
	public void updateMain(Scmpo scmpo, List<Scmpoitem> scmpoitemList, List<Scmpoattach> scmpoattachList) {
		scmpoMapper.updateById(scmpo);

		//1.先删除子表数据
		scmpoitemMapper.deleteByMainId(scmpo.getId());
		scmpoattachMapper.deleteByMainId(scmpo.getId());

		//2.子表数据重新插入
		if(scmpoitemList!=null && scmpoitemList.size()>0) {
			for(Scmpoitem entity:scmpoitemList) {
				//外键设置
				entity.setParentid(scmpo.getId());
				scmpoitemMapper.insert(entity);
			}
		}
		if(scmpoattachList!=null && scmpoattachList.size()>0) {
			for(Scmpoattach entity:scmpoattachList) {
				//外键设置
				entity.setParentid(scmpo.getId());
				scmpoattachMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		List<Scmpoitem> scmpoitems = scmpoitemMapper.selectByMainId(id.toString());
		for (Scmpoitem scmpoitem : scmpoitems) {
			String xjditemid = scmpoitem.getXjditemid();
			Scmxjditem scmxjditem = scmxjditemService.getById(xjditemid);
			scmxjditem.setPoflag("0");
			scmxjditemService.updateById(scmxjditem);
		}
		scmpoitemMapper.deleteByMainId(id);
		scmpoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			List<Scmpoitem> scmpoitems = scmpoitemMapper.selectByMainId(id.toString());
			for (Scmpoitem scmpoitem : scmpoitems) {
				String xjditemid = scmpoitem.getXjditemid();
				Scmxjditem scmxjditem = scmxjditemService.getById(xjditemid);
				scmxjditem.setPoflag("0");
				scmxjditemService.updateById(scmxjditem);
//				scmxjdService.updateById(scmxjd);

			}
			scmpoitemMapper.deleteByMainId(id.toString());
			scmpoMapper.deleteById(id);
		}
	}

	@Override
	@Transactional
	public void generatePO(List<String> ids) {
		QueryWrapper<Scmquotation> queryWrapper=new QueryWrapper<>();
		queryWrapper.in("id",ids);
		List<Scmquotation> scmquotations = scmquotationService.list(queryWrapper);
		scmquotations.forEach(item->{
			String fromitemid = item.getFromitemid();
			QueryWrapper<Scmpoitem> scmpoitemQueryWrapper = new QueryWrapper<>();
			scmpoitemQueryWrapper.eq("xjditemid",fromitemid);
			List<Scmpoitem> scmpoitems = scmpoitemMapper.selectList(scmpoitemQueryWrapper);
			if(scmpoitems.size()>0){
				throw new RuntimeException("已下达订单，不允许再次下达");
			}
		});
		Map<String, List<Scmquotation>> collect = scmquotations.stream().collect(Collectors.groupingBy(item -> item.getVendorid()+"&"+item.getFromid()));
		//根据供应商来分单
		for (String keyfromid : collect.keySet()) {
			List<Scmquotation> vendorQuoItems = collect.get(keyfromid);
			String[] split = keyfromid.split("&");
			Scmpo scmpo=new Scmpo();
			scmpo.setBilldate(ERPUtils.getLocalDate());
			scmpo.setBillcode(scmbillcoderuleService.getBillCode("po"));
			scmpo.setVendorid(split[0]);
			Scmxjd scmxjd = scmxjdService.getById(split[1]);
			scmpo.setCityid(scmxjd.getCityid());
			scmpo.setFullcituname(scmxjd.getFullcityname());
			scmpo.setXjdbillcode(scmxjd.getBillcode());
			scmpo.setAccidentNumber(scmxjd.getAccidentNumber());
			scmpo.setDeliveyflag("0");
			scmpo.setDsy(scmxjd.getCreatebyname());
			scmpo.setPlate(scmxjd.getPlate());
			scmpo.setAccidentimagesString(scmxjd.getAccidentimagesString());
			scmpo.setLicenseimageString(scmxjd.getLicenseimageString());
			scmpo.setXlclink(scmxjd.getXlclink());
			scmpo.setXlcaddress(scmxjd.getXlcaddress());
			LoginUser user = sysBaseAPI.getUserByName(scmxjd.getCreatebyname());
			if(user!=null){
				scmpo.setDsylinktel(user.getPhone());
      } else {
        scmpo.setDsylinktel("");
			}
			scmpoMapper.insert(scmpo);
			for (Scmquotation vendorQuoItem : vendorQuoItems) {
				Scmpoitem scmpoitem=new Scmpoitem();
				scmpoitem.setParentid(scmpo.getId());
				scmpoitem.setMatcode(vendorQuoItem.getMatcode());
				scmpoitem.setMatname(vendorQuoItem.getMatname());
				scmpoitem.setMatcount(vendorQuoItem.getCount());
				scmpoitem.setQuanlity(vendorQuoItem.getQuality());
				scmpoitem.setPrice(vendorQuoItem.getQuoprice());
				scmpoitem.setValue(vendorQuoItem.getQuovalue());
				scmpoitem.setFromitemid(vendorQuoItem.getId());
				scmpoitem.setXjdid(vendorQuoItem.getFromid());
				scmpoitem.setXjditemid(vendorQuoItem.getFromitemid());
				scmpoitemMapper.insert(scmpoitem);
				Scmxjditem scmxjditem = scmxjditemService.getById(scmpoitem.getXjditemid());
				scmxjditem.setPoflag("1");
				scmxjditemService.updateById(scmxjditem);
				scmxjd.setStatus("8");
				scmxjdService.updateById(scmxjd);
			}
		}
	}

}
